import turtle


mazelist = [['+', '+', '+', '+', '+', '+', '+', '+', '+', '+'],
            ['+', '-', '-', '+', '+', '+', '+', '+', '+', 'o'],
            ['+', '+', '-', '+', '+', '+', '+', '+', '+', '-'],
            ['+', '+', '+', '+', '+', '+', '+', '+', '-', '-'],
            ['+', '+', '+', '+', '+', '-', '-', '+', '-', '+'],
            ['+', '+', '+', '+', '+', '-', '+', '+', '-', '+'],
            ['+', '+', '-', '+', '-', 's', '-', '-', '-', '+'],
            ['+', '+', '+', '+', '-', '-', '+', '+', '+', '+'],
            ['+', '+', '+', '+', '+', '-', '+', '+', '+', '+'],
            ['+', '+', '+', '+', '+', '-', '+', '+', '+', '+']
            ]



class MazeMap(object):
    #以整数(m,n)为中心绘制一个边长为l的单元格,并根据symbol填充不同颜色
    def drawCell(m, n, l, symbol,shows, mypen):
        shows=symbol
        scolour={"+":"#eeeeee","-":"#FFFFFF","s":"#00ff00","o":"#0000ff","--":"#ff0000"}
        mypen.penup()
        mypen.goto(m-l/2, n+l/2)
        mypen.pendown()
        mypen.begin_fill()
        mypen.fillcolor(scolour[symbol])
        mypen.goto(m-l/2, n-l/2)
        mypen.goto(m+l/2, n-l/2)
        mypen.goto(m+l/2, n+l/2)
        mypen.goto(m-l/2, n+l/2)
        mypen.end_fill()
        mypen.penup()
        mypen.goto(m, n)
        mypen.write(shows)

    def __init__(self,mazelist,pen):
        #当前位置和初始位置
        self.localx,self.localy=-1,-1
        self.homex,homey=-1,-1
        #出口位置
        self.outx,self.outy=-1,-1
        self.mazelist=mazelist
        self.pen=pen
        for i in range(10):
            for j in range(10):
                if self.mazelist[i][j]=="s":
                    self.homex,self.homey=i,j
                    self.localx,self.localy=i,j
                if self.mazelist[i][j]=="o":
                    self.outx,self.outy=i,j

    
    def __getitem__(self,x):
        return self.mazelist[x]

    def setsymbol(self,x,y,s):
        self.mazelist[x][y]=s

    def getsymbol(self,x,y):
        return self.mazelist[x][y]

    def setlocalsymbol(self,symbol):
        self.mazelist[self.localx][self.localy]=symbol

    def location(self):
        return self.localx,self.localy

    #向四个方向移动，确定移动的位置可以通行，且不是走过的道路！！！
    def moveleft(self):
        self.setlocalsymbol("--")
        if self.localy>0 and self.getsymbol(self.localx,self.localy-1)=="-":
            #此路可通！
             return self.localx,self.localy
        else:
            return False
    def moveright(self):
        self.setlocalsymbol("--")
        if self.localy<9 and self.getsymbol(self.localx,self.localy+1)=="-":
            self.localy+=1
            return self.localx,self.localy
        else:
            return False
    def moveup(self):
        self.setlocalsymbol("--")
        if self.localx>0 and self.getsymbol(self.localx-1,self.localy)=="-":
            self.localx-=1
            
            return self.localx,self.localy
        else:
            return False
    def movedown(self):
        self.setlocalsymbol("--")
        if self.localx<9 and self.getsymbol(self.localx+1,self.localy)=="-":
            self.localx+=1
            self.setlocalsymbol("--")
            return self.localx,self.localy
        else:
            return False
        
    def draw(self,redraw=False):
        if redraw:
            #先清空画布再绘图
            self.pen.clear()
        for i in range(10):
            #print(mazelist[i])
            for j in range(10):
                #print(mazelist[i][j],end="")
                if self.mazelist[i][j]=="s":
                    self.homex,self.homey=i,j
                    self.localx,self.localy=i,j
                MazeMap.drawCell(j*60-300,-i*60+300,60,self.mazelist[i][j],"("+str(i)+","+str(j)+")",self.pen)
            #print("")

def findway(mp:MazeMap):
        print(mp.localx,mp.localy)
        mp.draw(True)
        if mp.moveright():
            findway(mp)
        if mp.movedown():
            findway(mp)
        if  mp.moveup():
            findway(mp)

        if mp.moveleft():
            findway(mp)
        mp.setlocalsymbol("--")
        
turtle.tracer(False)
mypen = turtle.Pen()

mp=MazeMap(mazelist,mypen)
findway(mp)
turtle.done()  